package com.example.administrator.myparkingos.util;
import android.opengl.GLES20;

public class GLToolbox
{
	 public static int loadShader(int shaderType, String source) {
	        int shader = GLES20.glCreateShader(shaderType);
	        if (shader != 0) {
	            GLES20.glShaderSource(shader, source);
	            GLES20.glCompileShader(shader);
	            int[] compiled = new int[1];
	            GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0);
	            if (compiled[0] == 0) {
	                String info = GLES20.glGetShaderInfoLog(shader);
	                GLES20.glDeleteShader(shader);
	                shader = 0;
	                throw new RuntimeException("Could not compile shader " +
	                shaderType + ":" + info);
	            }
	        }
	        return shader;
	    }

	    public static int createProgram(String vertexSource,
	            String fragmentSource) {
	        int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
	        if (vertexShader == 0) {
	            return 0;
	        }
	        int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
	        if (pixelShader == 0) {
	            return 0;
	        }

	        int program = GLES20.glCreateProgram();
	        if (program != 0) {
	            GLES20.glAttachShader(program, vertexShader);
	            checkGlError("glAttachShader");
	            GLES20.glAttachShader(program, pixelShader);
	            checkGlError("glAttachShader");
	            GLES20.glLinkProgram(program);
	            int[] linkStatus = new int[1];
	            GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus,
	                    0);
	            if (linkStatus[0] != GLES20.GL_TRUE) {
	                String info = GLES20.glGetProgramInfoLog(program);
	                GLES20.glDeleteProgram(program);
	                program = 0;
	                throw new RuntimeException("Could not link program: " + info);
	            }
	        }
	        return program;
	    }

	    public static void checkGlError(String op) {
	        int error;
	        while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
	            throw new RuntimeException(op + ": glError " + error);
	        }
	    }

	    public static void initTexParams() {
	        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
	                GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
	        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D,
	                GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
	        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
	                GLES20.GL_CLAMP_TO_EDGE);
	        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
	                GLES20.GL_CLAMP_TO_EDGE);
	   }
}
